package Domain.forumProtcol;

import java.util.Iterator;
import java.util.Vector;

import objects.User;
import Domain.forumManager.ForumController;
import Domain.stompProtocol.ForumFrame;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;

public class AdderProtocol extends ProtocolHelper
{
	public AdderProtocol(StompClient inStompClient,ForumController f,ForumFrame frmFrame)
	{
		this._stompClient = inStompClient;
		this.forumController=f;
		forumfrm=frmFrame;
	}
	
	public String processMessage(StompFrame msg)
	{
		String ans="";
		String str = msg.getData();
		String nameOfUser = msg.getUser();
		
		if (msg.getData().startsWith("add user"))
		{

				forumController.createUser(nameOfUser);
				ans+=this.sendSecessMessage(nameOfUser) ;
			
		}
		else if (msg.getData().startsWith("add manager"))
		{
			forumController.createUser(nameOfUser);
			if(this.forumController.changeUserType(nameOfUser, 1)==1)
				ans +=this.sendSecessMessage(nameOfUser);
			else 
			{
				this.forumfrm.setErr(this.forumfrm.UNKOWNERR);
			ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
			}
		}
		else if (msg.getData().startsWith("remove user"))
		{
			if (nameOfUser != null)
			{
				int t = this.forumController.removeUser(nameOfUser);
				if (t == 1)
				{
					ans+=this.sendSecessMessage(nameOfUser) ;
				}
				else 
					forumfrm.setErr(forumfrm.ERRUserNotFound);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
			}
		}
		//add message and send notifiction to all the freind of the user 
		//if user doesn't found won't do nothing 
		// if message doesn't not save well send an error
		else if (msg.getData().startsWith("add message"))
		{
			
			System.out.println("Contact Of Message : "+getContentsOfMessage(str));
			 //if we want to print the contact of the message
			
			
			try 
			{ //for the case one of them null
				Vector<User> friends = this.forumController.addMessage(getNameOfForum(str), nameOfUser, getNameOfThread(str),getContentsOfMessage(str));
				
				forumfrm.setFriendPublishedMessage(nameOfUser,getNameOfForum(str),getNameOfThread(str));
				
				for (Iterator<User> iterator = friends.iterator(); iterator.hasNext();) 
				{
					User user = iterator.next();
					String n = user.get_nameOfUser();
					
					ans += this.sendPlayerMsg(n,forumfrm);
				}
				forumfrm.setUserPublishedMessage(nameOfUser,getNameOfForum(str),getNameOfThread(str));
				
				//we will make all user know someone publish a message if the are in the same thread 
				//of him they will pop up a message to the user /
				Vector<String> allUsers=this.forumController.getAllUsersAsString();
				for (Iterator iterator = allUsers.iterator(); iterator
						.hasNext();)
				{
					String n = (String) iterator.next();
					if (!n.equals(nameOfUser))
						ans += this.sendPlayerMsg(n,forumfrm);
					
				}
				ans+=ans+=this.sendSecessMessage(nameOfUser) ;
			} 
			catch (RuntimeException e) 
			{
				ans+=this.InvalidMessage(nameOfUser);
			}
	
		}
		else if (msg.getData().startsWith("remove message"))
		{
			
			try 
			{ //for the case one of them null
				int t=this.forumController.removeMessage(getNameOfForum(str), nameOfUser, getNameOfThread(str),getContentsOfMessage(str));
				switch (t)
				{ 
					case 1:ans+=this.sendSecessMessage(nameOfUser);
					//System.out.println("1 ++++++++++++++ here");
					break;
					case -1:
						this.forumfrm.setErr(this.forumfrm.ERRMsseageNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-1 ++++++++++++++ here");
					break;
					case -2 :
						//System.out.println("-2 ++++++++++++++ here");
						this.forumfrm.setErr(this.forumfrm.ERRThreadNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
					break;
					case -3 : 
						this.forumfrm.setErr(this.forumfrm.ERRForumNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-3 ++++++++++++++ here");
					break;
					case -4: 
						this.forumfrm.setErr(this.forumfrm.ERRUserNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-4 ++++++++++++++ here");
					break;
					case -5:
						this.forumfrm.setErr(this.forumfrm.ERRInvalidTypeOfUSer);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-5 ++++++++++++++ here");
					break;
					
				}
			} 
			catch (RuntimeException e) 
			{
				ans+=this.InvalidMessage(nameOfUser);
				System.out.println("CATCH ++++++++++++++ here");
			}
	
		}
		else if (msg.getData().startsWith("add thread"))
		{
			if (nameOfUser != "")
			{
				try
				{
					int t = this.forumController.addThread(getNameOfForum(str), nameOfUser, getNameOfThread(str));
					switch (t)
					{
						case 1:ans+=this.sendSecessMessage(nameOfUser);
						break;
						case -1: 
							this.forumfrm.setErr(this.forumfrm.ERRUserNotFound);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						break;
						case -2 : 
							this.forumfrm.setErr(this.forumfrm.ERRInvalidTypeOfUSer);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						break;
						case -3 : 
							this.forumfrm.setErr(this.forumfrm.ERRForumNotFound);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						break;
					}
				}
				catch (RuntimeException e) 
				{
					ans+=this.InvalidMessage(nameOfUser);
				}
			}
			else
			{
				System.out.println(" inviled format of message\n");
			}
		}
		
		
		else if (msg.getData().startsWith("add forum"))
		{
			int temp=this.forumController.addForum(getNameOfForum(str), nameOfUser);
			if( temp== -2)
			{
				this.forumfrm.setErr(this.forumfrm.ERRInvalidTypeOfUSer);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
			}
			else if (temp==-1)
			{
				this.forumfrm.setErr(ForumFrame.ERRUserNotFound);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
			}
			
				
			else ans += this.sendSecessMessage(nameOfUser);
		}
		
		else if (msg.getData().startsWith("add friend"))
		{
			try
			{
				//I have used get name of forum becouse he return the second argument that
				//in this situation is the other friend
				int x = this.forumController.addFreind(nameOfUser, getNameOfForum(str));
				if (x == -1){
					this.forumfrm.setErrUserNotFound(getNameOfForum(str));
					ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
				}
				else 
					ans += sendSecessMessage(nameOfUser);
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);
			}
		}
		else if (msg.getData().startsWith("remove friend"))
		{
			try
			{
				//I have used get name of forum becouse he return the second argument that
				//in this situation is the other friend
				int x = this.forumController.removeFriend(nameOfUser, getNameOfForum(str));
				switch (x)
				{
					case 1: ans += sendSecessMessage(nameOfUser);
					break;
					case -1:
						this.forumfrm.setErrUserNotFound(nameOfUser);
						ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
					break;
					case -2 :
					this.forumfrm.setErr(forumfrm.ERRUserNotYourFriend);
					//you need to implement it becouse this not sappose to appen
					ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
					
					//:ans += this.sendPlayerMsg(nameOfUser, "the user  "+getNameOfForum(str)+" has not a friend of you");
					break;
					default :
						this.forumfrm.setErr(this.forumfrm.UNKOWNERR);
						ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
					break;
				}
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);
			}
		}
		
		return ans;
		
	}
}
